package com.loginactivity.qingyuan;

import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.format.Formatter;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import java.io.File;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private EditText et_name,et_mima;
    private Button btn;
    private CheckBox cb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_name= (EditText) findViewById(R.id.et_name);
        et_mima= (EditText) findViewById(R.id.et_mima);
        btn= (Button) findViewById(R.id.btn);
        cb= (CheckBox) findViewById(R.id.cb);

        File f= Environment.getExternalStorageDirectory();
        long sdSpace=f.getTotalSpace();
        long userSpace=f.getUsableSpace();
        String formatsdSpace= Formatter.formatFileSize(this,sdSpace);
        String formatuserSpace=Formatter.formatFileSize(this,userSpace);
        Map<String,String> maps  =UserSaveInfo.readInfo();
        if (maps!=null){
            String name=maps.get("name");
            String mima=maps.get("mima");
            et_name.setText(name);
            et_mima.setText(mima);
        }

    }
    public void login(View v){
        String name=et_name.getText().toString().trim();
        String mima=et_mima.getText().toString().trim();

        if (TextUtils.isEmpty("name")||TextUtils.isEmpty("mima")){
            Toast.makeText(this,"name和mima不能为空",Toast.LENGTH_SHORT).show();
        }else {
            if (cb.isChecked()){
                boolean result=UserSaveInfo.saveInfo(name,mima);
                if (result){
                    Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(this,"保存失败",Toast.LENGTH_SHORT).show();
                }
            }else {
                Toast.makeText(this,"请勾选cb",Toast.LENGTH_SHORT).show();
            }
        }
    }
}
